package org.jinxing.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jinxing.dao.BorrowsInfoDAO;
import org.jinxing.dao.RolesInfoDAO;
import org.jinxing.model.BorrowsInfoExample;
import org.jinxing.model.RolesInfo;
import org.jinxing.model.UsersInfo;
import org.jinxing.service.UsersInfoService;
import org.jinxing.utils.JsonUtils;
import org.jinxing.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/lg")
public class LoginController {
	protected static String className = LoginController.class.getName();
	protected static Log logger = LogFactory.getLog(className);//日志对象

	@Autowired
	BorrowsInfoDAO borrowsInfoDAO;
	@Autowired
	UsersInfoService usersInfoService;
	@Autowired
	RolesInfoDAO rolesInfoDAO;
	
	@RequestMapping("/lgIn")
	public String lgIn(HttpServletRequest request,HttpServletResponse response,Model model){
		logger.info("<|>"+className+"<|>lgIn<|><|><|>");
		String backUrl = request.getParameter("back");
		
		model.addAttribute("backUrl", backUrl);
		
		UsersInfo user = ServletUtils.getLgUser(request);
		
		if(null == user){
			return "/lg/lgIn";
		}
		if(null != backUrl && !backUrl.contains("lgOut")){
			try {
				backUrl = java.net.URLDecoder.decode(backUrl, "utf-8");
			} catch (UnsupportedEncodingException e) {
			}
		}else{
			backUrl = "/";
		}
		return "redirect:"+backUrl;
	}
	
	@RequestMapping("/login")
	public @ResponseBody String login(HttpServletRequest request,HttpServletResponse response){
		logger.info("<|>"+className+"<|>login<|><|><|>");
		String lgName = request.getParameter("lgName");
		String passwd = request.getParameter("passWd");
		Map<String,Object> result = new HashMap<String,Object>();
		UsersInfo user = usersInfoService.getUsersInfo(lgName, passwd);
		if(null == user){
			result.put("isLg", false);
		}else{
			result.put("isLg", true);
			ServletUtils.registerLgUser(request, user);
		}
		return JsonUtils.jsonFromObject(result);
	}
	
	@RequestMapping("/lgOut")
	public String lgOut(HttpServletRequest request,HttpServletResponse response){
		logger.info("<|>"+className+"<|>lgOut<|><|><|>");
		ServletUtils.removeLgUser(request);
		return "/lg/lgIn";
	}
	
	protected int availNum(Integer userId){
		int allNum = this.usersInfoService.getById(userId).getMaxnum();
		BorrowsInfoExample borrowsInfoExample = new BorrowsInfoExample();
		BorrowsInfoExample.Criteria borrowsC = borrowsInfoExample.createCriteria();
		borrowsC.andBorrowerEqualTo(userId);
		borrowsC.andIsbackEqualTo(0);//未归还书籍
		int availNum = allNum - this.borrowsInfoDAO.selectCountByExample(borrowsInfoExample);
		return availNum;
	}
	
	@RequestMapping("/lgUser")
	public @ResponseBody String lgUser(HttpServletRequest request,HttpServletResponse response){
		UsersInfo user = ServletUtils.getLgUser(request);
		user.setMaxnum(availNum(user.getId()));
		RolesInfo roles = rolesInfoDAO.selectByPrimaryKey(user.getRoleid());
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("user", user);
		map.put("role", roles);
		return JsonUtils.jsonFromObject(map);
	}
	
	
}
